package com.fantasy.servlet;


import com.fantasy.entity.Favorites;
import com.fantasy.entity.Page;
import com.fantasy.entity.Video;
import com.fantasy.service.VideoService;
import com.fantasy.service.impl.VideoServiceImpl;
import com.fantasy.utils.CookieUtils;
import com.fantasy.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@WebServlet("/client/favorite.do")
public class CFavoriteServlet extends BaseServlet {
    VideoService videoService=new VideoServiceImpl();

    /**
     * 删除视频
     * @param request
     * @param response
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Cookie[] cookies = request.getCookies();
        String userId = CookieUtils.getCookie("userId", cookies);
        Long id = Long.valueOf(request.getParameter("id"));
        int result = videoService.favorite_delete(id);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(result);
        response.sendRedirect("./favorite.do?action=page&userId=" + userId);
    }

    public void page(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int user_id = WebUtils.parseInt(request.getParameter("userId"),1);
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), 4);
        Page<Video> page = videoService.FavoritePage(user_id, pageNo, pageSize);

        //基于 Ajax
//        response.setContentType("text/html;charset=utf-8");
//        Gson gson = new Gson();
//        String s=gson.toJson(page);
//        response.getWriter().write(s);

        //基于 JSP
        request.setAttribute("page", page);
        request.getRequestDispatcher("/client/favorite.jsp").forward(request, response);
    }

    /**
     * 收藏夹ajax
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void pageAjax(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int user_id = WebUtils.parseInt(request.getParameter("userId"),1);
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), 4);
        Page<Video> page = videoService.FavoritePage(user_id, pageNo, pageSize);

        //基于 Ajax
        response.setContentType("application/json;charset=utf-8");
        Gson gson = new Gson();
        String s=gson.toJson(page);
        response.getWriter().write(s);
    }

    /**
     * 添加收藏
     * @param request
     * @param response
     */
    public void insert(HttpServletRequest request, HttpServletResponse response){
        Favorites favorites = new Favorites();
        favorites.setUserId(Integer.valueOf(request.getParameter("userId")));
        favorites.setVideoId(Integer.valueOf(request.getParameter("videoId")));
        response.setContentType("text/html;charset = utf-8");
        //查看是否添加过了
        if (videoService.queryFavoritesByUserIdAndVideoId(Integer.valueOf(request.getParameter("userId")),Integer.valueOf(request.getParameter("videoId")))==null){
            int count = videoService.favorite_insert(favorites);
            try {
                //1代表成功添加
                response.getWriter().write("1");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write("0");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
